import {
  Body,
  Controller,
  Get,
  Param, ParseUUIDPipe,
  Patch,
  Post,
  Query, Res,
  UseGuards,
} from '@nestjs/common';
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { EvaluationService } from './evaluation.service';
import { AdminAuthGuard } from "@/common/auth/admin-auth.guard";
import {
  EvaluationAdminIndexResult, EvaluationAdminShowResult,
} from "@/module/evaluation/evaluation.result";
import { PageResult } from "common/interface";
import { getPager } from "@/common/pager-response";
import { ApiPaginatedOkResponse } from "@/common/pager-response/api-paginated-ok-response.decorator";
import {
  EvaluationAdminCreateDto,
  EvaluationAdminIndexDto,
  EvaluationAdminUpdateDto
} from "@/module/evaluation/evaluation.dto";
import { Response } from 'express';

@Controller('/v1/admin/evaluations')
@ApiTags('Evaluation_admin')
@UseGuards(AdminAuthGuard)
export class EvaluationV1AdminController {
  constructor(private readonly evaluationService: EvaluationService) {
  }

  @Get('/')
  @ApiOperation({ summary: '列表查询' })
  @ApiPaginatedOkResponse(EvaluationAdminIndexResult)
  async index(@Query() query: EvaluationAdminIndexDto): Promise<PageResult<EvaluationAdminIndexResult>> {
    const res = await this.evaluationService.adminIndex(query);
    return getPager(res[0],res[1]);
  }

  @Get('/export-raw-fr-data')
  @ApiOperation({ summary: '导出频响数据' })
  async exportRawFrData(@Res() response: Response): Promise<void> {
    const res = await this.evaluationService.exportRawFrData();
    response.header(
      'Content-Type',
      'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    );
    response.attachment(
      `耳机频响数据.xlsx`,
    );
    response.send(res);
  }

  @Get('/evaluation/:uuid')
  @ApiOperation({ summary: '详情' })
  @ApiOkResponse({ type: EvaluationAdminShowResult })
  async showEvaluation(@Param('uuid', ParseUUIDPipe) uuid: string): Promise<EvaluationAdminShowResult | null> {
    const res = await this.evaluationService.adminShow(uuid);
    return res;
  }

  @Post('/')
  @ApiOperation({ summary: '新增评测文章' })
  async createEvaluationArticle(
    @Body() body: EvaluationAdminCreateDto,
  ): Promise<void> {
    await this.evaluationService.adminCreate(body);
    return;
  }

  @Patch('/:uuid')
  @ApiOperation({ summary: '编辑评测文章' })
  async updateEvaluationArticle(
    @Body() body: EvaluationAdminUpdateDto,
    @Param('uuid', ParseUUIDPipe) uuid: string,
  ): Promise<void> {
    await this.evaluationService.adminUpdate(body, uuid);
    return;
  }
}
